var salt = sessionStorage.getItem('usersalt');

var riskid = getQueryVariable('id');
var arr2 = new Array();
var danweim = '';

$('#resclas').click(function() {
	$('#resclass').show();
	$('#resclas').val('')
	$.fn.zTree.init($("#resclass"), setting, zNodes);
});

var company;

getallent();

function getallent() {

	$.ajax({
		url: url + 'baseEnt/getAllEnt',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {

		},
		success: function(data) {
			$('#resclass').empty();
			if(data.code == 0) {
				var list = data.data;
				company = list;

			}

		},
		error: function(error) {
			console.log("获取失败");
		}
	});

}

var setting = {
	check: {
		enable: true
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	check: {
		enable: true,
		chkStyle: "radio", //单选框
		radioType: "all" //对所有节点设置单选
	},
	callback: {
		onCheck: zTreeOnCheck
	}

};

var zNodes = company;

$.fn.zTree.init($("#resclass"), setting, zNodes);

$('#getval').click(function() {
	getval();
})

function zTreeOnCheck(event, treeId, treeNode) {
	var checked = treeNode.checked;
	danweim = treeNode ? treeNode.name : "root"
	$('#resclas').val(danweim)
	$('#resclass').hide();

};

function getval() {
	arr2 = [];
	danweim = '';
	var treeObj = $.fn.zTree.getZTreeObj("resclass");
	var nodes = treeObj.getCheckedNodes(true),

		v = "";
	for(var i = 0; i < nodes.length; i++) {
		v += nodes[i].name + ",";

		arr2.push(nodes[i].id); //获取选中节点的值
	}
	return arr2;
}

layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		table = layui.table;
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);

	}
	/*时间选择*/
	laydate.render({
		elem: '#checktime',
		trigger: 'click',
		max: 0
	});

	//*获取所有企业*/
	//     riskcompay();
	//     function riskcompay(){
	//     		$.ajax({
	//			url: url+'baseEnt/getAllEnt',
	//	      		dataType: 'json',
	//	      		async: false,
	//	      		type: "POST",
	//	      		data:{
	//	      			Token:salt
	//	      		},
	//	      		success: function(data) {
	//	      			
	//	      			$('#userdutycompay').empty();
	//	      			console.log(data);
	//	      			if(data.code==0){
	//	      				var list=data.data;
	//	      				var opt='<option value="0">请选择</option>'
	//	      				for(var i=0;i<list.length;i++){
	//	      					opt+='<option value="'+list[i].id+'" >'+list[i].name+'</option>'
	//	      				}
	//	      				
	//	      				$('#userdutycompay').append(opt);
	//	      				form.render();
	//	      			}
	//	
	//	      		},
	//	      		error: function(error) {
	//	      			console.log("获取失败");
	//	      		}
	//	      	});
	//     }

	/*通过隐患id获取隐患消息*/
	findinfo();

	function findinfo() {
		$.ajax({
			url: url + 'perilInfo/getInfo',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				Token: salt,
				id: riskid
			},
			success: function(data) {
				if(data.code == 0) {
					var list = data.data.perilInfo;
					$('#finduser').text(list.findUser); //发现人
					$('#findtime').text(list.findTime); //发现时间
					$('#typeId').text(list.typeName); //隐患类型
					$('#describe').text(list.describe); //隐患描述

					for(var i = 0; i < list.attachments.length; i++) {
						//	      					$('#descrimg').append('<img style="margin-right:20px;" src="'+url+"attachment/seekFile/"+list.attachments[i].id+'" width="100px"  />')
						$('#descrimg').append('<div style="display:inline-block;position:relative;margin:10px;" ><img id="pic' + list.attachments[i].id + '" onclick="lookbigpic(\'' + list.attachments[i].id + '\')" src="' + url + 'attachment/seekFile/' + list.attachments[i].id + '" width="100px" height="100px" class="picsty"/></div>')
					}

					$('#cheuser').text(list.auditUser); //核查人
					$('#chetime').text(list.auditTime); //核查时间

					$('#comptime').text(list.finishTime); //防控措施完成时间

					$('#cheaudi').text(list.auditFruit); //核查结论
					$('#auditStep').text(list.auditStep); //防控措施
					$('#leaderUser').text(list.leaderUser); //责任人

					form.render();

					var list1 = data.data.perilBills;

					for(var i = 0; i < list1.length; i++) {
						var flg = '';
						var goverfile = '';
						var schemepic = '';
						var result = '';
						/*获取文件*/
						if(list1[i].result == 0) {
							result = '<a style="color:#1c85d4">已销项</a>';
						} else if(list1[i].result == 1) {
							result = '未销项';
						} else {
							result = '';
						}
						//		      					$('#dutycompay').text(list1[i].leaderUnitsName);//责任单位
						//		      					$('#lushitime').text(list1[i].entTime)
						for(var l = 0; l < list1[i].attachmentsFile.length; l++) {

							goverfile += '<a style="color:#1c85d4; text-decoration:underline;" href="' + url + 'attachment/seekFile/' + list1[i].attachmentsFile[l].id + '">' + list1[i].attachmentsFile[l].originaName + '</a>&nbsp;&nbsp;';
						}
						for(var h = 0; h < list1[i].attachmentsImg.length; h++) {
							//		      						schemepic+='<img style="margin-right:20px;" src="'+url+'attachment/seekFile/'+list1[i].attachmentsImg[h].id+'" width="100px"  />';
							schemepic += '<div style="display:inline-block;position:relative;margin:10px;" ><img id="pic' + list1[i].attachmentsImg[h].id + '" onclick="lookbigpic(\'' + list1[i].attachmentsImg[h].id + '\')" src="' + url + 'attachment/seekFile/' + list1[i].attachmentsImg[h].id + '" width="100px" height="100px" class="picsty"/></div>';
						}
						flg = '<tr><tr><td style="padding:0px;" colspan="6"><hr class="layui-bg-black"></td></tr>' +
							'<tr><td class="fowei">责任人</td><td><span  id="leaderUser" >' + list1[i].leaderUser + '</span></td><td class="fowei">责任单位</td>' +
							'<td><span id="dutycompay">' + list1[i].leaderUnitsName + '</span></td><td class="fowei">治理措施<br />落实时间</td>' +
							'<td><span id="lushitime" class="line-H40">' + list1[i].entTime + '</span></td></tr>' +
							'<tr><td class="fowei">治理方案</td>' +
							'<td colspan="5"><span style="display: -webkit-box;word-break: break-all;-webkit-box-orient: vertical;overflow: hidden;text-overflow:ellipsis;">' + list1[i].program + '</span></td>' +
							'</tr><tr><td></td><td colspan="5"><div id="goverfile">' + goverfile + '</div></td></tr>' +
							'<tr><td class="fowei">治理后照片</td><td colspan="5"><div id="schemepic">' + schemepic + '</div></td></tr>';

						if(list1[i].type == 1) {
							flg = '<tr><tr><td style="padding:0px;" colspan="6"><hr class="layui-bg-black"></td></tr>' +
								'<tr><td><div class="layui-inline"><label class="fowei"><label class="fowei">销项核查人</label> </label></td><td>' +
								'<span >' + list1[i].leaderUser + '</span>' +
								'</div></td><td><div class="layui-inline"><label class="fowei">核查人单位</label></td><td>' +
								'<span >' + list1[i].leaderUnitsName + '</span>' +
								'</div></td><td><div class="layui-inline"><label class="fowei">治理措施<br />落实时间 </label></td><td>' +
								'<span >' + list1[i].entTime + '</span></div></td></tr><tr><td><label class="fowei">销项结论</label></td><td colspan="5"><span>' + result + '</span><td></tr>';
						}
						$('#newsdata').append(flg);
					}

				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}

	/*增加治理方案*/
	$('#upsubmit').on('click', function() {
		addgovernparti();
	});

	//     function verifyinfo(){
	//     	var box=/\s/g;
	//     	
	//     	var xiaocheuser=$('#xiaocheuser').val();//负责人
	//     	xiaocheuser=xiaocheuser.replace(box,'');
	//     		var userdutycompay=$('#userdutycompay').val();//责任单位
	//     		userdutycompay=userdutycompay.replace(box,'');
	//     		var checktime=$('#checktime').val();//落实时间
	//     		checktime=checktime.replace(box,'');
	//     		var governscheme=$('#governscheme').val()//治理方案
	//     		governscheme=governscheme.replace(box,'');
	//     		
	//     		if(userdutycompay>0&&checktime!=0&&xiaocheuser>0&&governscheme>0){
	//     			addgovernparti();
	//     		}else{
	//     			layer.msg('请将信息填写完整');
	//     		}
	//     		
	//     }
	function addgovernparti() {
		var xiaocheuser = $.trim($('#xiaocheuser').val()); //负责人
		//     		var userdutycompay=$('#userdutycompay').val();//责任单位
		var userdutycompay = getval().toString(); //责任单位
		var checktime = $('#checktime').val(); //落实时间

		if(xiaocheuser != '' && userdutycompay != 0 && checktime != '') {
			$.ajax({
				url: url + 'perilInfo/addBill',
				dataType: 'json',
				async: false,
				type: "POST",
				data: {
					Token: salt,
					id: riskid,
					leaderUser: xiaocheuser, //负责任人、核查人
					leaderUnits: userdutycompay, //负责单位、核查单位
					entTime: checktime, //落实时间、核查时间
					result: checkstatus,
					type: '1'

				},
				success: function(data) {
					if(data.code == 0) {
						layer.confirm('隐患销项核查提交成功！', {
							btn: ['确定'] //按钮
						}, function() {
							location.href = "page/risk/risk-list/risk-list.html";
							layer.closeAll();
						}, function() {

						});
					}

				},
				error: function(error) {
					console.log("获取失败");
				}
			});
		} else {
			layer.msg('请将信息填写完整');
		}

	}

	/*按钮样式*/
	var checkstatus = 0;
	var i = 0;
	$('#btnyes').on('click', function() {

		i++;
		if(i % 2 == 1) {
			$('#triangle').css('display', 'block');
			$('#btnyes').css('background', 'white');
			$('#triangle1').css('display', 'none');
			$('#btnno').css('background', 'gray');
			k = 0;
			checkstatus = 0;
		} else {
			$('#triangle').css('display', 'none');
			$('#btnyes').css('background', 'gray');
			$('#triangle1').css('display', 'block');
			$('#btnno').css('background', 'white');
			checkstatus = 1;
		}
	});
	var k = 0;
	$('#btnno').on('click', function() {
		k++;
		if(k % 2 == 1) {
			$('#triangle1').css('display', 'block');
			$('#btnno').css('background', 'white');
			$('#triangle').css('display', 'none');
			$('#btnyes').css('background', 'gray');
			checkstatus = 1;
			i = 0;
		} else {
			$('#triangle1').css('display', 'none');
			$('#btnno').css('background', 'gray');
			$('#triangle').css('display', 'block');
			$('#btnyes').css('background', 'white');
			checkstatus = 0;
		}
	});

});

function lookbigpic(self) {

	var json = {
		"status": 1,
		"msg": "",
		"title": "",
		"id": 8,
		"start": 0,
		"data": [{
			"alt": "",
			"pid": 109,
			"src": url + "attachment/seekFile/" + self,
			"thumb": ""
		}]
	}
	layer.photos({
		photos: json,
		anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
	});

}